package com.project.webpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.webpage.aop.TagLog;
import com.project.webpage.dto.SearchByRqParam;
import com.project.webpage.dto.SkbDailyDto;
import com.project.webpage.dto.SkbParam;
import com.project.webpage.service.SkbDailyService;
import com.project.webpage.service.SkbService;
import com.project.webpage.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/webpage/skb/")
public class SkbController extends BaseController {

    @Resource
    SkbService skbService;
    @Resource
    SkbDailyService skbDailyService;

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询时刻表列表")
    public RestResult page(@RequestBody SkbParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(skbService.getListForPage(param)));
    }

    /**
     * 导出列表
     */
    @PostMapping("output")
    @TagLog(operation = 5, content = "导出时刻表列表")
    public RestResult output(@RequestBody SkbParam param) {
        this.getPage(false, param);
        return RestResult.ok(skbService.getListForPage(param));
    }

    /**
     * 时刻表详细查询
     */
    @PostMapping("detail")
    @TagLog(operation = 0, content = "查询时刻表详细")
    public RestResult detailById(@RequestBody SkbParam param) {
        this.getPage(false, param);
        SkbDailyDto dto = new SkbDailyDto();
        List<SkbDailyDto> list = skbService.getById(param);
        if (list.size() > 0) {
            dto = list.get(0);
        }
        return RestResult.ok(dto);
    }

    /**
     * 时刻表班次详细查询
     */
    @PostMapping("bc/detail")
    @TagLog(operation = 0, content = "查询时刻表班次详细")
    public RestResult detailBcById(@RequestBody SkbParam param) {
        this.getPage(false, param);
        return RestResult.ok(skbService.getBcListById(param));
    }

    /**
     * 时刻表报备提交与撤回
     */
    @PutMapping("update")
    @TagLog(operation = 3, content = "时刻表报备提交与撤回")
    public RestResult update(@RequestBody SkbParam param) {
        this.getPage(false, param);
        try {
            Integer count = skbService.getCountById(param.getId());
            if (count == 0) {
                return RestResult.error("该条数据不存在");
            } else {
                skbService.updateStateById(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 指定线路按日时刻表详细查询
     */
    @PostMapping("daily/jgxlid/detail")
    @TagLog(operation = 0, content = "查询指定线路按日时刻表详细")
    public RestResult detailByJgxlid(@RequestBody SearchByRqParam param) {
        this.getPage(false, param);
        SkbDailyDto dto = new SkbDailyDto();
        List<SkbDailyDto> list = skbDailyService.getListByJgxlidForPage(param);
        if (list.size() > 0) {
            dto = list.get(0);
        }
        return RestResult.ok(dto);
    }

    /**
     * 指定线路按日时刻表班次详细查询
     */
    @PostMapping("daily/bc/jgxlid/detail")
    @TagLog(operation = 0, content = "查询指定线路按日时刻表班次详细")
    public RestResult detailBcByJgxlid(@RequestBody SearchByRqParam param) {
        this.getPage(false, param);
        return RestResult.ok(skbDailyService.getDetailListByJgxlidForPage(param));
    }
}
